<?php
require_once 'Net/URL.php';
/**
 *  MyApp_ActionClass.php
 *
 *  @author     TinhBV
 *  @package    Smscom
 *  @version
 */

/**
 *  actionclass
 *
 *  @author
 *  @package    Smscom
 *  @access     public
 */
class MyApp_ActionClass extends Ethna_ActionClass {
    var $_toppage_setting_obj;
    
    var $url_handler;
    
    var $uh;
    
    var $_flash = array();
    
    var $_flash_now = array();
    
    /**
     *  Ethna_ActionClass
     *
     *  @access public
     *  @param  object  Ethna_Backend   $backend    backendオブジェクト
     */
    function MyApp_ActionClass(&$backend)
    {
        parent::Ethna_ActionClass($backend);
        $c =& Ethna_Controller::getInstance();
        $this->url_handler =& $c->getUrlHandler();
        $this->uh = $this->url_handler;
    }
    
    /**
     *
     *  @access private
     */
    function _handleFlash()
    {
        $prev_flash = $this->session->get('__flash');
        
        if (!empty($prev_flash) && is_array($prev_flash)) $this->_flash_now = $prev_flash;
    }
    
    /**
     *  flash
     *
     *  @access public
     *  @param string  $name   flash
     *  @param mixed   $value  flash
     *  @param boolean $now    true
     */
    function setFlash($name, $value, $now = false)
    {
        if ($now) {
            $this->_flash_now[$name] = $value;
        } else {
            $this->_flash_now[$name] = $value;
            $this->_flash[$name] = $value;
            
            if (!$this->session->isStart(true)) {
                $this->session->start();
            }
        }
    }
    
    /**
     *  flash
     *
     *  @access public
     *  @param string  $name  flash
     *  @return mixed  $name flash
     */
    function getFlash($name)
    {
        return $this->_flash_now[$name];
    }
    
    /**
     * PC
     *
     * @param object $acc
     */
    function _checkMailMagazineAccess($acc)
    {
        if (Mobile_Util::isMobile()) {
            return;
        }
        
        $add_point = false;
        $mmc_get = isset($_GET['mmc']) ? $_GET['mmc'] : null;
        $mmc_cookie = isset($_COOKIE['mmc']) ? $_COOKIE['mmc'] : null;
        
        if ($mmc_get) {
            if ($acc->isMember()) {
                $add_point = true;
            } else {
                setcookie("mmc", $mmc_get, 0, '/');
            }
        } else if ($mmc_cookie) {
            if ($acc->isMember()) {
                $add_point = true;
                setcookie("mmc", '', time() - 3600, '/');
            }
        }
        
        if ($add_point) {
            $db = $this->backend->getDB('');
            $db->db->autocommit(false);
            $db->begin();
            
            $user_id = $this->session->get('user_id');
            $result = $this->backend->getManager('Point')->addPointDataByAction($user_id, 'MAIL_LOGIN');
            if (Ethna::isError($result)) {
                $db->rollback();
            } else {
                $db->commit();
            }
        }
    }

    /**
     *
     *  @access public
     *  @return string
     */
    function authenticate()
    {
        $this->af->setApp('base_url', $this->config->get('base_url'));
        $this->af->setApp('base_ssl_url', $this->config->get('base_ssl_url'));
        if (empty($_SERVER['HTTPS'])) {
            $this->af->setApp('base_scheme_url', $this->config->get('base_url'));
        } else {
            $this->af->setApp('base_scheme_url', $this->config->get('base_ssl_url'));
        }
        $acc = new MyApp_AccessControll($this->backend);
        
        $acc->mmAutoLogin();
		$this->_removeMmcMmk();
        if(!$acc->tryAutoLogin()){
		    $c =& Ethna_Controller::getInstance();
        	$action_name = $c->getCurrentActionName();
        	
            if (Mobile_Util::isDoCoMo() && !eregi('^help', $action_name) ) {
                if($_SERVER["REQUEST_METHOD"] != "POST"){
                    if($_SERVER ["HTTP_X_SSL_REQUEST"] != "on"){
                        if(!isset($_GET['guid'])){
                            $redirectUrl = substr($this->config->get('base_url'),0 , strlen($this->config->get('base_url'))-1).$_SERVER['REQUEST_URI'];
                            if(strpos($redirectUrl, '?')){
                                $guidOn = '&guid=ON';
                            }else{
                                $guidOn = '?guid=ON';
                            }
                            header('location: '.$redirectUrl.$guidOn.$queryString);
                            exit;
                        }
                    }
                }
            }
        }
        
        $this->_handleFlash();
        
        if ($acc->isOpenAdminArea() && !$acc->isOpenAdmin()) {
            return 'admin_login_index';
        }
        if ($acc->isAdminArea() && !$acc->isAdmin()) {
            return 'admin_login_index';
        }
        
        if (!$acc->isMember()) {
            return 'login';
        }
        
        if ($acc->isLogin()) {
            if (!$acc->isAdminArea() && !$acc->isOpenAdminArea()) {
                $this->af->setApp('base_url', $this->config->get('base_url') . 'member/');
                $this->af->setApp('base_ssl_url', $this->config->get('base_ssl_url') . 'member/');
                if (empty($_SERVER['HTTPS'])) {
                    $this->af->setApp('base_scheme_url', $this->config->get('base_url') . 'member/');
                } else {
                    $this->af->setApp('base_scheme_url', $this->config->get('base_ssl_url') . 'member/');
                }
            } elseif ($acc->isOpenAdminArea()) {
            	$this->af->setApp('base_url', $this->config->get('base_ssl_url') . 'openadmin/');
                $this->af->setApp('base_ssl_url', $this->config->get('base_ssl_url') . 'openadmin/');
                $this->af->setApp('base_scheme_url', $this->config->get('base_ssl_url') . 'openadmin/');
            } else {
                $this->af->setApp('base_url', $this->config->get('base_ssl_url') . 'admin/'); // 管理サイトは全てSSL通信にする
                $this->af->setApp('base_ssl_url', $this->config->get('base_ssl_url') . 'admin/');
                $this->af->setApp('base_scheme_url', $this->config->get('base_ssl_url') . 'admin/');
            }
            
            $is_login = $this->session->get('is_login');
            $user_id = $this->session->get('user_id');
            $user_profile_obj = $this->backend->getObject('UserProfile', 'id', $user_id);
            if (!$user_profile_obj->isValid() || $user_profile_obj->get('display_type') == 0) {
                return 'login_index';
            }
            
            $user = array();
            $user['is_login'] = $is_login;
            $user['user_id'] = $user_id;
            $user['authorization_type'] = $user_profile_obj->get('authorization_type');
            $user['user_name'] = $user_profile_obj->get('nickname');
            $user['nickname'] = $user_profile_obj->get('nickname');
            $user['user_image_file'] = $user_profile_obj->get('user_image_file');
            $user['total_point'] = $user_profile_obj->get('total_point');
            $user['member_type'] = $user_profile_obj->get('member_type');
            $user['work_type_id'] = $user_profile_obj->get('work_type_id');
            $user['self_introduction'] = $user_profile_obj->get('self_introduction');
            if (Mobile_Util::isMobile()) {
                $user['is_set_easylogin'] = $this->session->get('is_set_easylogin');
            }
            if (Smscom_Util::isOpenAdminArea()) {
            	$user['is_open_admin_area'] = 1;
            }
            $this->af->setApp('user', $user);
        }
        
        return parent::authenticate();
    }
    
    /**
     *
     *  @access public
     *  @return boolean true:, false:
     */
    function isLogin()
    {
        $user = $this->af->getApp('user');
        if ($user && $user['is_login']) {
            return true;
        }
        return false;
    }
    
    /**
     *
     *  @access public
     *  @return mixed ID:, null:
     */
    function getUserId()
    {
        $user = $this->af->getApp('user');
        if ($user && $user['user_id']) {
            return $user['user_id'];
        }
        return null;
    }

    /**
     *
     *  @access public
     *  @return string  (null, false)
     */
    function prepare()
    {
        return parent::prepare();
    }

    /**
     *
     *  @access public
     *  @return string
     */
    function perform()
    {
        return parent::perform();
    }
    
    /**
     * process url
     * @access public
     * @author luvina
     * @return url
     */
    function _removeMmcMmk(){
    	$mmc = isset($_GET['mmc']) ? $_GET['mmc'] : null;
        $mmk = isset($_GET['mmk']) ? $_GET['mmk'] : null;
        
        if (!empty($mmc) || !empty($mmk)) {
	    	if($_SERVER['HTTPS']) {
	    		$url = $this->config->get('base_ssl_url');
	    	} else {
	    		$url = $this->config->get('base_url');
	    	}
	    	
	    	$url = rtrim($url, '/');
	    	
	    	$url .= $_SERVER['REQUEST_URI'];
	    	
			$url = new Net_URL($url);
		
			$url->removeQueryString("mmc");
			$url->removeQueryString("mmk");	
			if(Mobile_Util::isMobile() && $this->session->isStart()){
				$url->addQueryString($this->session->session_name, session_id());
			}
			header('location: ' . $url->getURL());
	        exit();
        }
    }
    /**
     * returnError404
     * @return unknown_type
     */
    function returnError404 () {
        header("HTTP/1.0 404 Not Found");
        return 'error404';
    }
}
?>
